import java.io.*;

import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;

import org.antlr.runtime.tree.Tree;


public class Vsl {
    public static void main(String[] args) throws Exception {
        File f;
        FileReader s;
        PrintStream output;

        try {

            if ((args.length > 2) || (args.length == 0)) {
                System.err.println("Usage: Vsl fichier.vsl [fichier.s]");
                System.exit(1);
            };

            f = new File(args[0]);

            s = new FileReader(f);

            if (args.length == 2) {
                output = new PrintStream(new File(args[1]));
            } else {
                output = System.out;
            }

            ANTLRFileStream input = new ANTLRFileStream(args[0]);
            vslLexer lexer = new vslLexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            vslParser parser = new vslParser(tokens);

            vslParser.program_return r = parser.program();

            //resulting tree
            CommonTree t = (CommonTree)r.getTree();

            // Print the resulting tree out in LISP notation
            //System.out.println(t.toStringTree());

            CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
            vslTree tparser = new vslTree(nodes);

            Code c3a;

            try {
                c3a = tparser.program();

                c3a.print();
                CodeGenerator cg = new CodeGenerator(output);

                cg.genCodeMIPS(c3a);
            } catch (Exception e) {
                System.err.println(e.getMessage());
                e.printStackTrace();
            }

        } catch (FileNotFoundException fnf) {
            System.err.println("exception: " + fnf);
        }

    }
}
